<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="username"></el-input>
      <el-button>清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click.native="gett(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            @confirm="dellist(row.id)"
            title="确定删除任务吗？"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[5]"
        :page-size="params._limit"
        :current-page="params._page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="dialog">+ 新增</el-button>
      <!-- 新增弹层 -->
      <el-dialog title="提示弹层" :visible="dialogVisible" width="30%" @close="closeDialog">
        <el-form ref="form" label-width="120px" :model="Form" :rules="rules">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="Form.name" style="width: 80%" placeholder="请输入名称" />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="Form.des" style="width: 80%" placeholder="请描述一下" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click.native="confirm">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAPI, delAPI, addAPI, updatAPI, gettAPI, searchAPI } from './api/mianmian'
import { v4 as uuidv4 } from 'uuid'
export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: 0,
      username: '',
      Form: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '部任务编号不能为空', trigger: ['blur'] }],
        des: [{ required: true, message: '任务名称不能为空', trigger: ['blur'] }]
      }
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    async getlist() {
      const res = await getAPI(this.params)
      this.list = res.data
      console.log(res)
      this.total = Number(res.headers['x-total-count'])
    },
    handleSizeChange(val) {
      this.params._limit = val
      console.log(val)
      this.getlist()
    },
    handleCurrentChange(val) {
      this.params._page = val
      console.log(val)
      this.getlist()
    },
    async dellist(id) {
      await delAPI(id)
      // console.log(id)
      this.getlist()
    },
    closeDialog() {
      this.dialogVisible = false
    },
    dialog() {
      this.dialogVisible = true
      this.resform()
    },
    // 兜底校验
    confirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.Form.id) {
            await updatAPI(this.Form)
            this.$message.success(`编辑成功`)
          } else {
            this.Form.id = uuidv4() // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
            await addAPI(this.Form)
            this.$message.success(`新增成功`)
          }
          // this.$message.success(`${this.Form.id ? '编辑' : '新增'}成功`)
          this.getlist()
          this.dialogVisible = false
          this.$refs.form.resetFields()
          this.resform()
        }
      })
    },
    resform() {
      this.Form = {
        name: '',
        des: '',
        id: ''
      }
    },
    // 编辑
    async gett(id) {
      this.dialogVisible = true
      // console.log(id)
      const { data: res } = await gettAPI(id)
      this.Form = res
    },
    // 搜索
    async search() {
      const res = await searchAPI(this.username)
      this.list = res.data
      console.log(res)
      console.log(this.username)
      // this.getlist()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
